﻿#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define PI 3.1415926535
#define SAMPLE_RATE 44100 // 采样率（Hz）
#define DURATION 2        // 信号时长（秒）
#define MAX_SAMPLES SAMPLE_RATE * DURATION

// 生成正弦波载体信号（频率freq，幅度amp）
int generate_cover_signal(double* cover, const int size, double freq, double amp) {
    if (cover == NULL || size <= 0) return -1;
    for (int i = 0; i < size; i++) {
        double t = (double)i / SAMPLE_RATE; // 时间戳
        cover[i] = amp * sin(2 * PI * freq * t); // 正弦波公式
    }
    return 0;
}

// 生成数字调制信号（0/1随机序列）
int simulate_digital_modulation_signal(unsigned char* message, const int size) {
    if (message == NULL || size <= 0) return -1;
    srand(123); // 固定种子保证可复现
    for (int i = 0; i < size; i++) {
        message[i] = (rand() % 2 == 0) ? 0 : 1; // 随机0/1
    }
    return 0;
}

// 生成模拟调制信号（三角波）
int simulate_analog_modulation_signal(double* message, const int size, double freq) {
    if (message == NULL || size <= 0) return -1;
    for (int i = 0; i < size; i++) {
        double t = (double)i / SAMPLE_RATE;
        double cycle = t * freq;
        double phase = cycle - floor(cycle); // 0-1周期
        // 三角波：0-0.5上升，0.5-1下降
        message[i] = (phase < 0.5) ? (4 * phase - 1) : (3 - 4 * phase);
    }
    return 0;
}

// 数字调频（1对应高频，0对应低频）
int modulate_digital_frequency(double* cover, const int cover_len, const unsigned char* message, const int msg_len, double freq0, double freq1, double amp) {
    if (cover == NULL || message == NULL || cover_len <= 0 || msg_len <= 0) return -1;
    int samples_per_bit = cover_len / msg_len; // 每个bit的采样数
    for (int i = 0; i < msg_len; i++) {
        double freq = (message[i] == 1) ? freq1 : freq0; // 选择频率
        for (int j = 0; j < samples_per_bit; j++) {
            int idx = i * samples_per_bit + j;
            if (idx >= cover_len) break;
            double t = (double)idx / SAMPLE_RATE;
            cover[idx] = amp * sin(2 * PI * freq * t);
        }
    }
    return 0;
}

// 模拟调频（调制信号控制载波频率）
int modulate_analog_frequency(double* cover, const int cover_len, const double* message, const int msg_len, double base_freq, double freq_dev, double amp) {
    if (cover == NULL || message == NULL || cover_len <= 0 || msg_len <= 0) return -1;
    int samples_per_msg = cover_len / msg_len;
    for (int i = 0; i < msg_len; i++) {
        double freq = base_freq + freq_dev * message[i]; // 频率随调制信号变化
        for (int j = 0; j < samples_per_msg; j++) {
            int idx = i * samples_per_msg + j;
            if (idx >= cover_len) break;
            double t = (double)idx / SAMPLE_RATE;
            cover[idx] = amp * sin(2 * PI * freq * t);
        }
    }
    return 0;
}

// 数字调幅（1对应高幅度，0对应低幅度）
int modulate_digital_amplitude(double* cover, const int cover_len, const unsigned char* message, const int msg_len, double amp0, double amp1, double freq) {
    if (cover == NULL || message == NULL || cover_len <= 0 || msg_len <= 0) return -1;
    int samples_per_bit = cover_len / msg_len;
    for (int i = 0; i < msg_len; i++) {
        double amp = (message[i] == 1) ? amp1 : amp0; // 选择幅度
        for (int j = 0; j < samples_per_bit; j++) {
            int idx = i * samples_per_bit + j;
            if (idx >= cover_len) break;
            double t = (double)idx / SAMPLE_RATE;
            cover[idx] = amp * sin(2 * PI * freq * t);
        }
    }
    return 0;
}

// 模拟调幅（调制信号控制载波幅度）
int modulate_analog_amplitude(double* cover, const int cover_len, const double* message, const int msg_len, double base_amp, double amp_dev, double freq) {
    if (cover == NULL || message == NULL || cover_len <= 0 || msg_len <= 0) return -1;
    int samples_per_msg = cover_len / msg_len;
    for (int i = 0; i < msg_len; i++) {
        double amp = base_amp + amp_dev * message[i]; // 幅度随调制信号变化
        for (int j = 0; j < samples_per_msg; j++) {
            int idx = i * samples_per_msg + j;
            if (idx >= cover_len) break;
            double t = (double)idx / SAMPLE_RATE;
            cover[idx] = amp * sin(2 * PI * freq * t);
        }
    }
    return 0;
}

// 数字调相（1对应相位跳变180°，0对应相位不变）
int modulate_digital_phase(double* cover, const int cover_len, const unsigned char* message, const int msg_len, double freq, double amp) {
    if (cover == NULL || message == NULL || cover_len <= 0 || msg_len <= 0) return -1;
    int samples_per_bit = cover_len / msg_len;
    double phase = 0; // 初始相位
    for (int i = 0; i < msg_len; i++) {
        if (message[i] == 1) phase += PI; // 1对应相位跳变180°
        for (int j = 0; j < samples_per_bit; j++) {
            int idx = i * samples_per_bit + j;
            if (idx >= cover_len) break;
            double t = (double)idx / SAMPLE_RATE;
            cover[idx] = amp * sin(2 * PI * freq * t + phase);
        }
    }
    return 0;
}

// 模拟调相（调制信号控制载波相位）
int modulate_analog_phase(double* cover, const int cover_len, const double* message, const int msg_len, double freq, double phase_dev, double amp) {
    if (cover == NULL || message == NULL || cover_len <= 0 || msg_len <= 0) return -1;
    int samples_per_msg = cover_len / msg_len;
    for (int i = 0; i < msg_len; i++) {
        double phase = phase_dev * message[i]; // 相位随调制信号变化
        for (int j = 0; j < samples_per_msg; j++) {
            int idx = i * samples_per_msg + j;
            if (idx >= cover_len) break;
            double t = (double)idx / SAMPLE_RATE;
            cover[idx] = amp * sin(2 * PI * freq * t + phase);
        }
    }
    return 0;
}

// 测试：生成调制信号并保存到文件（可通过MATLAB/Python绘图查看）
int main() {
    double cover[MAX_SAMPLES];
    unsigned char digital_msg[100]; // 数字调制信号（100个bit）
    double analog_msg[MAX_SAMPLES]; // 模拟调制信号

    // 1. 生成数字调制信号并调频
    simulate_digital_modulation_signal(digital_msg, 100);
    modulate_digital_frequency(cover, MAX_SAMPLES, digital_msg, 100, 1000, 2000, 0.8);
    FILE* f1 = fopen("digital_fm.txt", "w");
    for (int i = 0; i < MAX_SAMPLES; i++) fprintf(f1, "%lf\n", cover[i]);
    fclose(f1);

    // 2. 生成模拟调制信号并调幅
    simulate_analog_modulation_signal(analog_msg, MAX_SAMPLES, 10);
    modulate_analog_amplitude(cover, MAX_SAMPLES, analog_msg, MAX_SAMPLES, 0.5, 0.3, 1000);
    FILE* f2 = fopen("analog_am.txt", "w");
    for (int i = 0; i < MAX_SAMPLES; i++) fprintf(f2, "%lf\n", cover[i]);
    fclose(f2);

    printf("调制信号已保存到文件\n");
    return 0;
}